package com.aaa.hospital.util;

import cn.hutool.db.Session;
import com.aaa.hospital.sysconst.LoginConst;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class LoginFilterUtil {
    public static void loginFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;

        String contextPath = req.getContextPath();
        String requestURI = req.getRequestURI();

        String path = requestURI.replace(contextPath, "");

        String[]  pass = { "/", "/login.html", "/index.html"};
        List<String> passList = Arrays.asList(pass);

        if(   ! passList.contains( path )  ){
            Object admin = req.getSession().getAttribute(LoginConst.EMP_IN_SESSION);
            if(admin == null){

                resp.sendRedirect("/ssm/index.html");
                return;
            }
        }
        filterChain.doFilter(req,resp);
    }
}